//
//  CreateSendOrderCell.m
//  FactorySale
//
//  Created by 主事丫环HU on 2020/7/28.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "CreateSendOrderCell.h"
#import "SYBigImage.h"
@interface CreateSendOrderCell()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *partsNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *countTextField;
@property (weak, nonatomic) IBOutlet UILabel *unitLabel;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;

@end
@implementation CreateSendOrderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.countTextField.delegate = self;
    self.priceTextField.delegate = self;
    
    [self.countTextField addTarget:self action:@selector(faHuoNumChange) forControlEvents:UIControlEventEditingChanged];
    [self.priceTextField addTarget:self action:@selector(priceChange) forControlEvents:UIControlEventEditingChanged];
}
- (void)faHuoNumChange {
    
    if (self.countTextField.text.length >=8) {
        self.countTextField.text = [self.countTextField.text substringToIndex:8];
    }
    if (self.faHuoNumTFEndHandle) {
        self.faHuoNumTFEndHandle(self.countTextField.text);
    }
}
- (void)priceChange {
    
    if (self.priceTextField.text.length >=8) {
        self.priceTextField.text = [self.priceTextField.text substringToIndex:8];
    }
    if (self.priceChangedEndHandle) {
           self.priceChangedEndHandle(self.priceTextField.text);
       }
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    
    static NSString *ID = @"CreateSendOrderCell";
    
    CreateSendOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [NSBundle.mainBundle loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)setModel:(SelectProductListModel *)model{
    
    __weak typeof(self) _weakSelf = self;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.attributeLogo] placeholderImage:imgname(GOODS_PLACE_HOLDER) completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image && cacheType == SDImageCacheTypeNone) {
            _weakSelf.iconImageView.alpha = 0;
            [UIView animateWithDuration:1.0 animations:^{
                _weakSelf.iconImageView.alpha = 1.0f;
            }];
        } else {
            _weakSelf.iconImageView.alpha = 1.0f;
        }
    }];
    SYBigImage * bigI = [[SYBigImage alloc]init];
    [self.iconImageView addGestureRecognizer:bigI];
    self.iconImageView.userInteractionEnabled = YES;
    
    self.partsNameLabel.text = model.attributeName;
    self.countTextField.text = model.quantity;
    self.unitLabel.text = model.unit;
    self.priceTextField.text = [NSString stringWithFormat:@"%.2f",model.price];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.countTextField) {// 限制数量输入必须为数字
        NSArray *attt = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
        if (![attt containsObject:string]  && ![string  isEqualToString:@""]) {
            ShowToastWithText(@"只能输入数字");
            return NO;
        }else{
            return YES;
        }
    } else {// 限制价格输入为小数点后两位
        //新输入的
        if (string.length == 0) {
            return YES;
        }
        NSString *checkStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
        //正则表达式（只支持两位小数）
        NSString *regex = @"^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$";
        //判断新的文本内容是否符合要求
        return [self isValid:checkStr withRegex:regex];
    }
}
- (BOOL) isValid:(NSString*)checkStr withRegex:(NSString*)regex
{
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicte evaluateWithObject:checkStr];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
